<?php
require "header.php";
require "authenticatie.php";

// Toevoegen
if( isset( $_POST['ADD'] ) && ereg('^[1-9][0-9]*$', $_POST['aantal'])) {
	$_SESSION['winkelwagen'][$_POST['artikelid']] = $_POST['aantal'];
}
// Verwijderen
if( isset( $_GET['DEL'] ) ) {
	unset($_SESSION['winkelwagen'][$_GET['DEL']]);
}
// Laat lijst zien
if( $_SESSION['winkelwagen'] ) {
	$totaal = 0;
	echo "<p><div class=\"big\">De Winkelwagen</div><br>";
	echo "<table border=2 cellpadding=0 cellspacing=0>";
	echo "<tr><td><b>Aantal</b></td><td><b>Naam</b></td><td><b>Prijs in &euro;</b></td><td>Bewerken</td></tr>"; // bovenste regel
	foreach ( $_SESSION['winkelwagen'] as $key=>$val ) {

		$sql = "SELECT * FROM Artikel WHERE ARTIKEL_ID=$key";

		$resultaat = mysql_query($sql);

		while ($rij = mysql_fetch_array($resultaat)) {
			echo "<tr>";
			echo "<td>$val</td>";
			echo "<td>".$rij["Naam"]."</td>";
			echo "<td>".number_format($rij["Prijs"], 2, ',', ' ')."</td>";
			echo "<td><a href=\"".$_SERVER["PHP_SELF"]."?DEL=$key\">[Verwijder]</a>";
			echo "</tr>";
			$totaal = $totaal + ($rij["Prijs"] * $val);
		}

	}
	echo "</table>";
	echo "<p>Totaalbedrag: &euro; ".number_format($totaal, 2, ',', ' ')."<br>";
	echo ">> <a href=\"bestelling.php\" onClick=\"return Confirm(0)\">[Bestelling uitvoeren]</a>";
} else {

	echo "De winkelwagen is momenteel leeg...";

}

if ($_GET["artikelid"]) {

	$sql = "SELECT * FROM Artikel WHERE ARTIKEL_ID=".$_GET["artikelid"];

	$result = mysql_query($sql);
	if (mysql_num_rows($result) > 0) {
		$naam = mysql_result($result, 0, "Naam");
		$prijs = number_format(mysql_result($result, 0, "Prijs"), 2, ',', ' ');
		echo "<p><div class=\"big\">Artikel toevoegen aan winkelwagen</div>";
		echo "<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">";
		echo "<input type=\"hidden\" name=\"artikelid\" value=\"".$_GET["artikelid"]."\">";
		echo "<input type=\"text\" name=\"aantal\" value=\"1\" size=\"2\" maxlength=2>";
		echo " x Artikel: $naam (prijs: $prijs euro)";
		echo "<input type=\"submit\" name=\"ADD\" value=\"Toevoegen\">";
		echo "</form>";
	}

}
require "footer.php";
?>